<h2>客户车辆管理</h2>
<button id="btn_caradds" class="layui-btn layui-btn-sm" style="margin: 10px; float:right;">新增用户</button>
<table id="tb_user" class="layui-table" lay-filter="user">
</table>
<script type="text/html" id="carbarDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    $("#btn_caradds").click(function () {
        layer.open({
            title: '新增客户',
            type:1,
            //调取弹出模板
            content:$("#hidden_re_car").html(),
            success:function () {
                layui.use(['form'], function () {
                    var form = layui.form;
                    form.on("submit(res_exitCar)",function(data){
                        var load = layer.load(1, {shade: 0.3});
                        $.post("/che/adds.do", data.field, function (data) {
                            layer.close(load);
                            if (data.code == 1) {
                                //重新加载
                                layui.table.reload('idTest');
                                layer.msg("新增成功!", {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        });

                        layer.close(index);
                    })


                });
            }
        });
    });

    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#tb_user',
            url: "/che/car.do",
            page: true,
            id: 'idTest',
            cols: [[
                {field: 'id', width: 180, sort: true, title: 'ID'},
                {field: 'car_id', sort: true, title: '车牌号'},
                {field: 'vin', sort: true, title: '大架号'},
                {field: 'car_type', sort: true, title: '车型'},
                {field: 'add_up', sort: true, title: '累计金额'},
                {field: 'm_name', sort: true, title: '车主姓名'},
                {field: 'm_tel', sort: true, title: '车主电话'},
                {fixed: 'right', width: 180, align: 'center', title: '操作', toolbar: '#carbarDemo'}
            ]]
        });
        //监听工具条
        table.on('tool(user)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var load = layer.load(1, {shade: 0.3});
                    $.get("/che/del.do", {id: data.id}, function (data) {
                        layer.close(load);
                        if (data.code == 1) {
                            layui.table.reload('idTest');
                            layer.msg("删除成功!", {icon: 1});
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    title: '客户信息修改',
                    type:1,
                    //调取html弹出模板
                    content:$("#hidden_re_carcar").html(),
                    success:function () {
                        $('input[name=id]').val(data.id);
                        $('input[name=carId]').val(data.car_id);
                        $('input[name=vin]').val(data.vin);
                        $('input[name=carType]').val(data.car_type);
                        $('input[name=addUp]').val(data.add_up);
                        $('input[name=mName]').val(data.m_name);
                        $('input[name=mTel]').val(data.m_tel);
                        layui.use(['form'],function () {
                            var form=layui.form;
                            form.on("submit(res_exitCarcar)",function (data) {
                                var load = layer.load(1, {shade: 0.3});
                                // console.log("1111111111111");
                                $.post("/che/edit.do",data.field,function (res) {
                                    layer.close(load);
                                    if (res.code == 1) {
                                        layui.table.reload('idTest');
                                        layer.msg("修改成功!", {icon: 1});
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                });

                            })

                        })

                    }
                });

            }
        });
    });
</script>
<!--        弹出模板        -->
<script type="text/html" id="hidden_re_car">
    <form class="layui-form" style="margin: 15px 30px 20px 0px;">
        <input type="hidden" name="id" >
        <div class="layui-form-item">
        <label class="layui-form-label">车牌号：</label>
        <div class="layui-input-block">
            <input type="text" name="carId" lay-verify="required" lay-verType="tips" autocomplete="off"
                   placeholder="请输入车牌号"
                   class="layui-input">
        </div>
    </div>
        <div class="layui-form-item">
            <label class="layui-form-label">大架号：</label>
            <div class="layui-input-block">
                <input type="text" name="vin" lay-verify="required" lay-verType="tips" autocomplete="off"
                       placeholder="请输入大架号"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车型：</label>
            <div class="layui-input-block">
                <input type="text" name="carType" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车型" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">累计金额：</label>
            <div class="layui-input-block">
                <input type="text" name="addUp" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入累计金额" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主姓名：</label>
            <div class="layui-input-block">
                <input type="text" name="mName" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车主姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主电话：</label>
            <div class="layui-input-block">
                <input type="text" name="mTel" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车主电话" class="layui-input">
            </div>
        </div>
        <div style="text-align: right;">
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button class="layui-btn" lay-submit="" lay-filter="res_exitCar">
                提交
            </button>
        </div>
    </form>
</script>
<!--修改模板-->
<script type="text/html" id="hidden_re_carcar">
    <form class="layui-form" style="margin: 15px 30px 20px 0px;">
        <input type="hidden" name="id" >
        <input type="hidden" name="carId" >
        <div class="layui-form-item">
            <label class="layui-form-label">大架号：</label>
            <div class="layui-input-block">
                <input type="text" name="vin" lay-verify="required" lay-verType="tips" autocomplete="off"
                       placeholder="请输入大架号"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车型：</label>
            <div class="layui-input-block">
                <input type="text" name="carType" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车型" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">累计金额：</label>
            <div class="layui-input-block">
                <input type="text" name="addUp" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入累计金额" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主姓名：</label>
            <div class="layui-input-block">
                <input type="text" name="mName" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车主姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主电话：</label>
            <div class="layui-input-block">
                <input type="text" name="mTel" lay-verify="repwd" autocomplete="off"
                       placeholder="请输入车主电话" class="layui-input">
            </div>
        </div>
        <div style="text-align: right;">
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button class="layui-btn" lay-submit="" lay-filter="res_exitCarcar">
                提交
            </button>
        </div>
    </form>
</script>
